<?php
/**
 * [PHPFOX_HEADER]
 */

defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package 		Phpfox_Component
 * @version 		$Id: profile.class.php 3826 2011-12-16 12:30:19Z Raymond_Benc $
 */
class User_Component_Controller_Profile extends Phpfox_Component
{
	/**
	 * Class process method wnich is used to execute this component.
	 */
	public function process()
	{
		Phpfox::isUser(true);
		$bIsEdit = false;
		
		$iUserId = Phpfox::getUserId();
		$iUserGroupId = Phpfox::getUserBy('user_group_id');
		if (($iId = $this->request()->getInt('id')) && Phpfox::getUserParam('custom.can_edit_other_custom_fields') && $iId != Phpfox::getUserId())
		{
			if (($aUser = Phpfox::getService('user')->getUser($iId, 'u.user_id, u.user_name, u.full_name')) && isset($aUser['user_id']))
			{
				$iUserId = $aUser['user_id'];
				$iUserGroupId = $aUser['user_group_id'];
				$this->template()->assign('iUserId', $iUserId);
				$bIsEdit = true;
										
				if ($aVals = $this->request()->getArray('custom'))
				{
					if (Phpfox::getService('custom.process')->updateFields($iUserId, $iUserId, $aVals))
					{
						$this->url()->send($aUser['user_name'], null, Phpfox::getPhrase('user.successfully_updated_full_name_profile', array('full_name' => $aUser['full_name'])));
					}
				}
			}
		}
		else 
		{
			Phpfox::getUserParam('custom.can_edit_own_custom_field', true);
		}
		
		
		
		//IMAGE
		if ($aVals = $this->request()->getArray('val'))
		{
			if(isset($aVals['uploaded_master']))
			{
				$aImage = Phpfox::getLib('file')->load('image2', array('jpg', 'gif', 'png'), (Phpfox::getUserParam('user.max_upload_size_profile_photo') === 0 ? null : (Phpfox::getUserParam('user.max_upload_size_profile_photo') / 1024)));
			}
			else
			{
				$aImage = Phpfox::getLib('file')->load('image', array('jpg', 'gif', 'png'), (Phpfox::getUserParam('user.max_upload_size_profile_photo') === 0 ? null : (Phpfox::getUserParam('user.max_upload_size_profile_photo') / 1024)));
			}
			if (!empty($aImage['name']))
			{
				$iUserId = Phpfox::getUserId();
			
				if(isset($aVals['uploaded_master']))
				{					
					if (($aImage = Phpfox::getService('pet')->uploadMasterImage($iUserId, (Phpfox::getUserParam('user.force_cropping_tool_for_photos') ? false : true)) != false))					
					{				
						$this->url()->send('user.profile', array('tab'=>'master'), Phpfox::getPhrase('user.profile_photo_successfully_uploaded'));					
					}
				}
				else
				{
					if (($aImage = Phpfox::getService('user.process')->uploadImage($iUserId, (Phpfox::getUserParam('user.force_cropping_tool_for_photos') ? false : true) ) != false))					
					{				
						$this->url()->send('user.profile', null, Phpfox::getPhrase('user.profile_photo_successfully_uploaded'));					
					}			
				}
				
			}
		}	
		
		$sImage = Phpfox::getLib('image.helper')->display(array(
				'server_id' => Phpfox::getUserBy('server_id'),
				'title' => Phpfox::getUserBy('full_name'),
				'path' => 'core.url_user',
				'file' => Phpfox::getUserBy('user_image'),
				'suffix' => '',
				'max_width' => 500,
				'max_height' => 500,
				'no_default' => true,
				'time_stamp' => true,
				'id' => 'user_profile_photo',				
				'class' => 'border'
			)
		);
		
		$sImageThumb = Phpfox::getLib('image.helper')->display(array(
				'server_id' => Phpfox::getUserBy('server_id'),
				'title' => Phpfox::getUserBy('full_name'),
				'path' => 'core.url_user',
				'file' => Phpfox::getUserBy('user_image'),
				'suffix' => '_120',
				'max_width' => 120,
				'max_height' => 120,
				'no_default' => true,
				'time_stamp' => true,
				'class' => 'border'	
			)
		);		
		
		$sImageAvatar = Phpfox::getLib('image.helper')->display(array(
				'server_id' => Phpfox::getUserBy('server_id'),
				'title' => Phpfox::getUserBy('full_name'),
				'path' => 'core.url_user',
				'file' => Phpfox::getUserBy('user_image'),
				'suffix' => '_75_square',
				'max_width' => 75,
				'max_height' => 75,
				'no_default' => true,
				'time_stamp' => true,
				'class' => 'border'
			)
		);
				
		$oPet = Phpfox::getService('pet');
		
		$sMasterImage = $oPet->getMasterImage($iUserId);

		$sImageAvatar2 = Phpfox::getLib('image.helper')->display(array(
				'server_id' => Phpfox::getUserBy('server_id'),
				'title' => Phpfox::getUserBy('full_name'),
				'path' => 'core.url_user',
				'file' => $sMasterImage,
				'suffix' => '_75_square',
				'max_width' => 75,
				'max_height' => 75,
				'no_default' => true,
				'time_stamp' => true,
				'class' => 'border'
			)
		);
		
		//END IMAGE
		
		$aCustomGroups = Phpfox::getService('custom.group')->getGroups('user_profile', $iUserGroupId);
		$aCustomFields = Phpfox::getService('custom')->getForEdit(array('user_main', 'user_panel', 'profile_panel'), $iUserId, $iUserGroupId, null, Phpfox::getUserId());
        //d($aCustomFields); die();
		$iGroupMasterId = isset($aCustomFields['cf_master_name']['group_id']) ? (int)$aCustomFields['cf_master_name']['group_id'] : 0;
		$aGroupCache = array();
		foreach ($aCustomFields as $aFields)
		{			
			$aGroupCache[$aFields['group_id']] = true;
		}		
		
		foreach ($aCustomGroups as $iKey => $aCustomGroup)
		{
			if (!isset($aGroupCache[$aCustomGroup['group_id']]))
			{
				unset($aCustomGroups[$iKey]);
			}
		}
		
		$sTab = $this->request()->get('tab', false);
		
		$aRebuildKeys = $aCustomGroups;
		$aCustomGroups = array();
		$iCnt = 0;
		foreach ($aRebuildKeys as $aCustomGroup)
		{
			$aCustomGroups[$iCnt] = $aCustomGroup;
			$iCnt++;
		}		
		
		
		$aTimeZones = Phpfox::getService('core')->getTimeZones();
		if (count($aTimeZones) > 100) // we are using the php 5.3 way
		{
			$this->template()->setHeader('cache', array('setting.js' => 'module_user'))
				->setHeader('cache', array(
					'<script type="text/javascript">sSetTimeZone = "' . Phpfox::getUserBy('time_zone') . '";</script>'
				)
			);
		}
		$aForms = Phpfox::getService('user')->get(Phpfox::getUserId(), true);
		/* we could put this part inside get but I fear its being wrongly used */
		$aRelation = Phpfox::getService('custom.relation')->getLatestForUser(Phpfox::getUserId(), null, true);		
		if (isset($aRelation['status_id']))
		{
			$aForms = array_merge($aForms, $aRelation);					
		}
		
		$sJsArray = '{';
		$aRelations = Phpfox::getService('custom.relation')->getAll();
		foreach ($aRelations as $aItem)
		{
			if ($aItem['confirmation'] == 1)
			{
				$sJsArray .= $aItem['relation_id'] . ':' . $aItem['confirmation'] .',';
			}
		}
		$sJsArray = rtrim($sJsArray, ',') .'}';
		
		
		
		
		$aForms['month'] = substr($aForms['birthday'], 0, 2);
		$aForms['day'] = substr($aForms['birthday'],2,2);
		$aForms['year'] = substr($aForms['birthday'],4);

		if (Phpfox::isModule('friend'))
		{
			$this->template()->setPhrase(array('friend.show_more_results_for_search_term'));
		}
		
		$this->template()->setTitle(Phpfox::getPhrase('user.edit_profile'))
			->setBreadcrumb(Phpfox::getPhrase('user.edit_profile'))
			->setFullSite()
			->setHeader(array(
					'country.js' => 'module_core',
					'custom.js' => 'module_custom',
					'search.js' => 'module_friend',
					'edit-profile.css' => 'module_user',
					'progress.js' => 'static_script',
					'<script type="text/javascript">$Behavior.changeUserPhoto = function(){ if ($Core.exists(\'#js_photo_form_holder\')) { oProgressBar = {holder: \'#js_photo_form_holder\', progress_id: \'#js_progress_bar\', uploader: \'#js_progress_uploader\', add_more: false, max_upload: 1, total: 1, frame_id: \'js_upload_frame\', file_id: \'image\'}; $Core.progressBarInit(); } }</script>',
					'<script type="text/javascript">$Behavior.changeUserPhoto2 = function(){ if ($Core.exists(\'#js_photo_form_holder2\')) { oProgressBar = {holder: \'#js_photo_form_holder2\', progress_id: \'#js_progress_bar2\', uploader: \'#js_progress_uploader2\', add_more: false, max_upload: 1, total: 1, frame_id: \'js_upload_frame2\', file_id: \'image2\'}; $Core.progressBarInit(); } }</script>'
				)
			)
		 	->assign(array(
		 			'aGroups' => $aCustomGroups,
		 			'aSettings' => $aCustomFields,
		 			'bIsEdit' => $bIsEdit,
					'sDobStart' => Phpfox::getParam('user.date_of_birth_start'),
					'sDobEnd' => Phpfox::getParam('user.date_of_birth_end'),
					'aTimeZones' => $aTimeZones,
					'aForms' => $aForms,
					'sJsArray' => $sJsArray,
					'iMaxFileSize' => (Phpfox::getUserParam('user.max_upload_size_profile_photo') === 0 ? null : ((Phpfox::getUserParam('user.max_upload_size_profile_photo') / 1024) * 1048576)),
					'aRelations' => Phpfox::getService('custom')->getRelations(),		 							
					'sProfileImage' => $sImage,
					'sImageThumb' => $sImageThumb,
					'sImageAvatar' => $sImageAvatar,
					'sProfileImage2' => str_replace('id="user_profile_photo"', 'id="js_profile_photo_preview"', $sImage),					
					'sImageAvatar2' => $sImageAvatar2,
					'iGroupMasterId' => $iGroupMasterId,
					'sMasterImage' => $sMasterImage,
					'sTab' => $sTab
				)			
		 	);
			
			
			if ((Phpfox::getUserBy('user_image') && !empty($sImage)))
			{
				if (Phpfox::getParam('core.allow_cdn') && Phpfox::getUserBy('server_id') > 0)
				{
					preg_match("/src=\"(.*?)\"/", $sImage, $aMatches);
	
					list($iWidth, $iHeight) = getimagesize($aMatches[1]);				
				}	
				else
				{		
					preg_match("/height=\"(.*?)\" width=\"(.*?)\"/", $sImage, $aMatches);			
					$iHeight = $aMatches[1];
					$iWidth = $aMatches[2];
				}
				$this->template()->setHeader('cache', array(
							'jquery/plugin/jquery.crop.js' => 'static_script',
							'<script type="text/javascript">$Core.photo_crop.init({width: 75, height: 75, image_width: ' . $iWidth . ', image_height: ' . $iHeight . '});</script>',							
						)
					)		
					->assign(array(
							'iImageHeight' => $iHeight,
							'iImageWidth' => $iWidth
						)
					);
			}
	}
	
	/**
	 * Garbage collector. Is executed after this class has completed
	 * its job and the template has also been displayed.
	 */
	public function clean()
	{
		(($sPlugin = Phpfox_Plugin::get('user.component_controller_profile_clean')) ? eval($sPlugin) : false);
	}
}

?>
